package com.epi.one.main;


import java.util.*;

/**
 * @author dxy
 * @version 1.0
 */
public class CollectionDemo {
    public static void main(String[] args) {
        test();
    }

    public static void test() {
        useQueue();
        forEachRemain();
        previousIterator();
        mapMethod();
        algBinary();
        algPrime();
        propertyMap();
    }

    private static void algPrime() {
        int max = 20000;
        BitSet bitSet = new BitSet(max + 1);

        bitSet.set(2, max + 1);

        for (int i = 2; i * i <= max; i++) {
            if (bitSet.get(i)) {
                for (int j = i * i; j <= max; j += i) {
                    bitSet.clear(j);
                }
            }
        }

        for (int i = 2; i <= max; i++) {
            if (bitSet.get(i)) {
                System.out.print(i + " ");
            }
        }
        System.out.println();
        System.out.println(bitSet.cardinality());
    }

    private static void propertyMap() {
        Properties properties = System.getProperties();
        System.out.println("=============  System Properties =============");
        properties.forEach((k,v)->{
            System.out.printf("【Key】%-30s  【Value】%-30s \n",k,v);
        });
        System.out.println("二级属性映射");
        Properties settings = new Properties(properties);
        System.out.println(settings);
        System.out.println(settings.getProperty("java.version"));

    }

    private static void algBinary() {
        List<Integer> l = List.of(1, 4, 6, 9);
        System.out.println(Collections.binarySearch(l, 0));
        System.out.println(Collections.binarySearch(l, 4));
        System.out.println(Collections.binarySearch(l, 5));
    }

    private static void mapMethod() {
        Map<String, Integer> map = new HashMap<>();
        map.put("key1", 10);
        map.merge("key1", 5, Integer::sum);  // 10+5=15
        map.compute("key1", (k, v) -> v == null ? 1 : v * 2);  // 15*2=30
        map.merge("key2", 5, Integer::sum); // 5
        map.compute("key3", (k, v) -> v == null ? 1 : v * 2); //1
        map.entrySet().forEach(System.out::println);
    }

    private static void previousIterator() {
        List<String> list = new ArrayList<>();
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");

        // 获取 ListIterator，初始位置在列表末尾（list.size()）
        ListIterator<String> iterator = list.listIterator(list.size());
        String d = iterator.previous();
        String c = iterator.previous(); // A B | C D
        iterator.remove(); //C被删除   A B | D
        iterator.add("E"); //E被加在游标左边  A B E | D
        System.out.println(iterator.previousIndex());
        System.out.println(iterator.nextIndex());
        String D = iterator.next();
        iterator.add("F");  // A B  E | F D

        list.forEach(System.out::print);

    }

    private static void forEachRemain() {
        Iterator<String> it = List.of("A", "B", "C").iterator();
        it.next(); // 先消费一个元素（"A"）
        it.forEachRemaining(s -> System.out.println("剩余: " + s));
    }

    private static void useQueue() {
        Queue<Integer> queue = new ArrayDeque<>();
        Queue<Integer> queue1 = new LinkedList<>();

        Deque<Integer> queue2 = new LinkedList<>();
        Deque<Integer> queue3 = new LinkedList<>();
    }
}
